# Change this to the location of the replication repository
setwd("D://Dropbox//Coalitions//CuesReplication")

### Table A1: Dichotomization robustness analysis
load("./cueingresults_84.Rdata")
base.fit84 <- fit
base.fit84$se <- sqrt(diag(dyad.vcov))
cbind(coef(base.fit84), base.fit84$se, coef(base.fit84)/base.fit84$se)[1:14,]

load("./cueingresults_126.Rdata")
base.fit126 <- fit
base.fit126$se <- sqrt(diag(dyad.vcov))
cbind(coef(base.fit126), base.fit126$se, coef(base.fit126)/base.fit126$se)[1:14,]

load("./partisan_cueingresults_84.Rdata")
part.fit84 <- fit
part.fit84$se <- sqrt(diag(dyad.vcov))
cbind(coef(part.fit84), part.fit84$se, coef(part.fit84)/part.fit84$se)[1:15,]

load("./partisan_cueingresults_126.Rdata")
part.fit126 <- fit
part.fit126$se <- sqrt(diag(dyad.vcov))
cbind(coef(part.fit126), part.fit126$se, coef(part.fit126)/part.fit126$se)[1:15,]
###

### Table A2: Cosponsorship as a count variable
load("./cueingresults_cont.RData")
cont.base.fit <- fit
cont.base.fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(cont.base.fit), cont.base.fit$se, coef(cont.base.fit)/cont.base.fit$se)[1:14,]

cont.part.fit <- part_fit
cont.part.fit$se <- sqrt(diag(part_dyad.vcov))
cbind(coef(cont.part.fit), cont.part.fit$se, coef(cont.part.fit)/cont.part.fit$se)[1:15,]
###

### Interference Analysis
interference_x <- read.csv(paste0("./interference105.csv"))
interference_x$deltay <- (interference_x$agree2 - interference_x$agree1)
interference_x$friend <- 0
for (i in 1:nrow(interference_x)){
  interference_x$friend[i] <- ifelse(interference_x$copartisans[i] == 1, ifelse(interference_x$cs[i] >= cocs, 1, 0), ifelse(interference_x$cs[i] >= oppcs, 1, 0))
}

Aind <- which(colnames(interference_x) == "legA")
Bind <- which(colnames(interference_x) == "legB")

interference_x$dyads <- apply(interference_x, 1, function(r) paste0(r[Aind], "-", r[Bind]))

interference_fmla <- formula(deltay ~ factor(Acuetaker + Bcuetaker) + friend + copartisans +
                               sgcs + ogcs + sfcs + ofcs + 
                               I(friend * copartisans) + 
                               interaction(factor(congress), committee, leg_party, copartisans) + 
                               n1 + n2)

interference_fit <- lm(interference_fmla, data = interference_x, weights = interference_x$weights)
coef(interference_fit)[1:8]
coef(interference_fit)[1:8]/sqrt(diag(vcov(interference_fit))[1:8])

weighted.mean((interference_x$Acuetaker + interference_x$Bcuetaker) == 1, interference_x$weights) +
  weighted.mean((interference_x$Acuetaker + interference_x$Bcuetaker) == 2, interference_x$weights)

# Insofar as the no interference assumption is violated, its effect on agreement rates in the control
# group is negligible.
weighted.mean((interference_x$Acuetaker + interference_x$Bcuetaker) == 1, interference_x$weights) * coef(interference_fit)[2] +
  weighted.mean((interference_x$Acuetaker + interference_x$Bcuetaker) == 2, interference_x$weights) * coef(interference_fit)[3]
###


### Table A9: Alternative cutpoints
load("./time_cueingresults_105102robust.RData")
fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(fit), fit$se, coef(fit)/fit$se)[c(1:17,478:479),]

load("./time_cueingresults_105103robust.RData")
fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(fit), fit$se, coef(fit)/fit$se)[c(1:17,478:479),]

load("./time_cueingresults_105105robust.RData")
fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(fit), fit$se, coef(fit)/fit$se)[c(1:17,478:479),]

load("./time_cueingresults_105106robust.RData")
fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(fit), fit$se, coef(fit)/fit$se)[c(1:17,478:479),]
###

### Table A10: DW-NOMINATE
load("./polar_cueingresults_105.RData")
fit$se <- sqrt(diag(dyad.vcov))
cbind(coef(fit), fit$se, coef(fit)/fit$se)[c(1:16,478:479),]
###